#!/usr/bin/env python
#coding=utf-8
from django import forms
from ishare.core.models import ShareItem
from ishare.core.models import Label
from django.core.files.storage import FileSystemStorage
from time import gmtime, strftime
from ishare import settings

class ShareItemForm(forms.Form):
    """
    名称,说明,图片.
    动态属性,其他图片
    """
    name = forms.CharField(max_length=100,label=u'分享名')
    image = forms.ImageField(label=u'图片')
    description = forms.CharField(widget=forms.Textarea(),label=u'说明')
    label = forms.CharField(max_length=200,widget=forms.TextInput({'size':56}),label=u'标签(请用空格分隔不同的标签)')
    
    def save(self,request):
        if request.user:
            ts = strftime('items/%Y/%m/%d/',gmtime())
            storage = FileSystemStorage(location=settings.MEDIA_ROOT + ts)
            path = storage.save(None,self.cleaned_data['image'])
            item = ShareItem(name=self.cleaned_data['name'],description=self.cleaned_data['description'],
                                image=ts + path,owner=request.user)
            # 保存标签
            Label.objects.save_from_str(item, self.cleaned_data['label'])
            
            item.save()
            
            return item

class PropertyForm(forms.Form):
    name = forms.CharField(max_length=100,label=u'属性名')
    value = forms.CharField(max_length=500,label=u'属性值')
    #item_id = forms.IntegerField(widget=forms.HiddenInput())

class ItemImageForm(forms.Form):
    image = forms.ImageField(label=u'图片')
    info = forms.CharField(label=u'描述',max_length=100)
    
    